from django.shortcuts import render
from django.http import JsonResponse, QueryDict
from kubernetes import client
from devops import k8s

# Create your views here.
@k8s.self_login_required
def pvc(requset):
    return render(requset, 'storage/pvc.html')

@k8s.self_login_required
def pvc_create(request):
    return render(request, 'storage/pvc_create.html')

@k8s.self_login_required
def pvc_api(requset):
    # 命名空间选择和命名空间列表功能使用
    auth_type = requset.session.get("auth_type")
    token = requset.session.get("token")
    namespace = requset.GET.get("namespace")
    data = []  # [{},{}]
    core_api = client.CoreV1Api()

    if requset.method == "GET":
        # 调用k8s api，获取命名空间，返回一个json数据
        search_key = requset.GET.get('search_key')
        try:
            for pvc in core_api.list_namespaced_persistent_volume_claim(namespace=namespace).items:
                name = pvc.metadata.name
                namespace = pvc.metadata.namespace
                labels = pvc.metadata.labels
                storage_class_name = pvc.spec.storage_class_name
                access_modes = pvc.spec.access_modes
                capacity = (pvc.status.capacity if pvc.status.capacity is None else pvc.status.capacity["storage"])
                volume_name = pvc.spec.volume_name
                status = pvc.status.phase
                create_time = k8s.dt_format(pvc.metadata.creation_timestamp)

                pvc = {"name": name, "namespace": namespace, "lables": labels,
                       "storage_class_name": storage_class_name, "access_modes": access_modes, "capacity": capacity,
                       "volume_name": volume_name, "status": status, "create_time": create_time}

                # 搜索，适配待条件查询
                if search_key:
                    if search_key in name:
                        data.append(pvc)
                else:
                    data.append(pvc)
            code = 0
            msg = "查询成功！"
        except Exception as e:
            status = getattr(e, 'status')
            if status == 403:
                msg = "没有访问权限！"
            else:
                msg = "查询失败！"
            code = 1
        count = len(data)
        # 适配命名空间选择
        if requset.GET.get('page'):
            current_page = int(requset.GET.get('page', 1))
            page_item_num = int(requset.GET.get('limit', 10))
            start = (current_page - 1) * page_item_num
            end = current_page * page_item_num
            data = data[start:end]
        result = {'code': code, 'msg': msg, 'count': count, 'data': data}
        return JsonResponse(result)
    elif requset.method == "POST":
        # 新增
        name = requset.POST.get("name", None)
        namespace = requset.POST.get("namespace", None)
        storage_class = requset.POST.get("storage_class", None)
        access_mode = requset.POST.get("access_mode", None)
        capacity = requset.POST.get("capacity", None)
        body = client.V1PersistentVolumeClaim(
            api_version="v1",
            kind="PersistentVolumeClaim",
            metadata=client.V1ObjectMeta(name=name, namespace=namespace),
            spec=client.V1PersistentVolumeClaimSpec(
                storage_class_name=storage_class,  # 使用存储类创建PV，如果不用可去掉
                access_modes=[access_mode],
                resources=client.V1ResourceRequirements(
                    requests={"storage": capacity}
                )
            )
        )
        try:
            core_api.create_namespaced_persistent_volume_claim(namespace=namespace, body=body)
            code = 0
            msg = "创建PersistentVolumeClaim成功！"
        except Exception as e:
            status = getattr(e, 'status')
            if status == 403:
                msg = "没有创建PersistentVolumeClaim权限！"
            else:
                msg = "创建PersistentVolumeClaim失败！"
            code = 1
        result = {'code': code, "msg": msg}
        return JsonResponse(result)
    elif requset.method == "DELETE":
        # 删除
        print(requset.body)
        requset_data = QueryDict(requset.body)
        name = requset_data.get("name")
        namespace = requset+data.get('namespace')
        try:
            core_api.delete_namespaced_persistent_volume_claim(namespace=namespace, name=name)
            code = 0
            msg = "删除成功！"
        except Exception as e:
            status = getattr(e, 'status')
            if status == 403:
                msg = "没有删除权限！"
            else:
                msg = "删除失败！"
            code = 1
        result = {'code': code, 'msg': msg}
        return JsonResponse(result)
    elif requset.method == "PUT":
        # 更新
        pass


@k8s.self_login_required
def configmap(requset):
    return render(requset, 'storage/configmap.html')

@k8s.self_login_required
def configmap_api(requset):
    # 命名空间选择和命名空间列表功能使用
    auth_type = requset.session.get("auth_type")
    token = requset.session.get("token")
    namespace = requset.GET.get("namespace")
    data = []  #[{},{}]
    k8s.load_auth_config(auth_type, token)
    core_api = client.CoreV1Api()

    if requset.method == "GET":
        # 调用k8s api，获取命名空间，返回一个json数据
        search_key = requset.GET.get('search_key')
        try:
            for cm in core_api.list_namespaced_config_map(namespace=namespace).items:
                name = cm.metadata.name
                namespace = cm.metadata.namespace
                data_length = ("0" if cm.data is None else len(cm.data))
                create_time = k8s.dt_format(cm.metadata.creation_timestamp)

                cm = {"name": name, "namespace": namespace, "data_length": data_length, "create_time": create_time}
                # 搜索，适配待条件查询
                if search_key:
                    if search_key in name:
                        data.append(cm)
                else:
                    data.append(cm)
            code = 0
            msg = "查询成功！"
        except Exception as e:
            status = getattr(e, 'status')
            if status == 403:
                msg = "没有访问权限！"
            else:
                msg = "查询失败！"
            code = 1
        count = len(data)
        # 适配命名空间选择
        if requset.GET.get('page'):
            current_page = int(requset.GET.get('page', 1))
            page_item_num = int(requset.GET.get('limit', 10))
            start = (current_page - 1) * page_item_num
            end = current_page * page_item_num
            data = data[start:end]
        result = {'code': code, 'msg': msg, 'count': count, 'data': data}
        return JsonResponse(result)
    elif requset.method == "POST":
        # 新增
        pass
    elif requset.method == "DELETE":
        # 删除
        print(requset.body)
        requset_data = QueryDict(requset.body)
        name = requset_data.get("name")
        namespace = requset_data("namespace")
        try:
            core_api.delete_namespaced_config_map(namespace=namespace, name=name)
            code = 0
            msg = "删除成功!"
        except Exception as e:
            status = getattr(e, 'status')
            if status == 403:
                msg = "没有删除权限！"
            else:
                msg = "删除失败！"
            code = 1
        result = {'code': code, "msg": msg}
        return JsonResponse(result)
    elif requset.method == "PUT":
        # 更新
        pass

@k8s.self_login_required
def secret(requset):
    return render(requset, 'storage/secrets.html')

@k8s.self_login_required
def secret_api(requset):
    # 命名空间选择和命名空间列表功能使用
    auth_type = requset.session.get("auth_type")
    token = requset.session.get("token")
    namespace = requset.GET.get("namespace")
    data = []  # [{},{}]
    k8s.load_auth_config(auth_type, token)
    core_api = client.CoreV1Api()

    if requset.method == "GET":
        # 调用k8s api，获取命名空间，返回一个json数据
        search_key = requset.GET.get('search_key')
        try:
            for secret in core_api.list_namespaced_secret(namespace=namespace).items:
                name = secret.metadata.name
                namespace = secret.metadata.namespace
                data_length = ("空" if secret.data is None else len(secret.data))
                create_time = k8s.dt_format(secret.metadata.creation_timestamp)

                se = {"name": name, "namespace": namespace,
                      "data_length": data_length, "create_time": create_time}
                # 搜索，适配待条件查询
                if search_key:
                    if search_key in name:
                        data.append(se)
                else:
                    data.append(se)
            code = 0
            msg = "查询成功！"
        except Exception as e:
            status = getattr(e, 'status')
            if status == 403:
                msg = "没有访问权限！"
            else:
                msg = "查询失败！"
            code = 1
        count = len(data)
        # 适配命名空间选择
        if requset.GET.get('page'):
            current_page = int(requset.GET.get('page', 1))
            page_item_num = int(requset.GET.get('limit', 10))
            start = (current_page - 1) * page_item_num
            end = current_page * page_item_num
            data = data[start:end]
        result = {"code": code, "msg": msg, "count": count, "data": data}
        return JsonResponse(result)
    elif requset.method == "POST":
        #新增
        pass
    elif requset.method == "DELETE":
        #删除
        print(requset.body)
        requset_data = QueryDict(requset.body)
        name = requset_data.get("name")
        namespace = requset_data.get("namespace")
        try:
            core_api.delete_namespaced_secret(namespace=namespace, name=name)
            code = 0
            msg = "删除成功！"
        except Exception as e:
            status = getattr(e, 'status')
            if status == 403:
                msg = "没有删除权限！"
            else:
                msg = "删除失败！"
            code = 1
        result = {'code': code, 'msg': msg}
    elif requset.method == "PUT":
        #更新
        pass